/**
 *  *2010-3-20 下午10:14:42  add 
 */
package com.base.myproject.client.busi.system;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.HR.bcode.BcodeSelectDialog;
import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.StaticMessage;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.StoreFilter;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.CheckColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Label;

/**
 * 业务权限
 * 
 * @author Administrator 2010-3-20 javazgw@gmail.com
 */
public class BusinessPermissions extends BaseJDBCPanel {

	Button addrecord = new Button("添加");

	Button delrecord = new Button("删除");
	BaseComboBoxForm<BaseModelData> C_num4 = null;
	BcodeSelectDialog bsd  = null;
	public BusinessPermissions() {
		init();
	}

	public void init() {
		ModelType combotype = new ModelType();
		combotype.setRoot("records");
		combotype.setRecordName("record");
		combotype.setTotalName("totle");
		combotype.addField("code");
		combotype.addField("name");

		StoreInfo comboinfo = new StoreInfo();
		comboinfo.setTablename("bcode");
		// comboinfo.setForcenolimit("yes");
		comboinfo.setForcenolimit("true");
		comboinfo.setSql("select a.bcode as code,b.employno %2B a.bname as name from bcode a,bcode_job b where a.bcode = b.bcode and (b.employstatus = '在职' or a.bcode ='root')");
		BaseStore<BaseModelData> bs = new BaseStore<BaseModelData>(combotype, comboinfo) {
			protected boolean isFiltered(ModelData record, String property) {
			    if (filterBeginsWith != null && property != null) {
			      Object o = record.get(property);
			      if (o != null) {
			        if (o.toString().toLowerCase().indexOf(filterBeginsWith.toLowerCase()) < 0) {
			          return true;
			        }
			      }
			    }
			    if (filters != null) {
			      for (StoreFilter filter : filters) {
			        boolean result = filter.select(this, record, record, property);
			        if (!result) {
			          return true;
			        }
			      }
			    }
			    return false;
			  }
		};
		
		C_num4 = new BaseComboBoxForm<BaseModelData>("code","name",bs,true);
		
		addrecord.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE
				.addrecord()));
		delrecord.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE
				.deleterecord()));

		addrecord.addListener(Events.Select, getListener());
		delrecord.addListener(Events.Select, getListener());

		this.setAutoHeight(true);
		// BorderLayoutData centerData = new
		// BorderLayoutData(LayoutRegion.CENTER);
		// centerData.setMargins(new Margins(5, 5, 0, 0));
		// this.add(initWeigetx(),centerData);
		//			
		this.setLayout(new FitLayout());
		this.add(initWeigetx());

		registerMainStore(store);
		registerStore(store);
		registerStore(gstore);
		// this.setBusino(busi);
	}

	protected void onRender(Element target, int index) {
		super.onRender(target, index);
		// getLayoutTarget().setStyleAttribute("background-image",
		// "url(images/baseimage/backgroup/logo.png)");
		// getLayoutTarget().setStyleAttribute("background-repeat",
		// "no-repeat");
		// this.el().setStyleAttribute("background-position", "center");
		// this.el().setStyleAttribute("background-color", "#CCD9E8");

	}

	private VerticalPanel initWeigetx() {
		VerticalPanel v = new VerticalPanel();
		v.setHorizontalAlign(HorizontalAlignment.LEFT);
		HorizontalPanel h = new HorizontalPanel();
		h.setVerticalAlign(VerticalAlignment.TOP);

		h.add(createGrid());
		h.add(createGGrid());
		v.add(h);
		v.add(createForm());

		return v;
	}

	// public ContentPanel initWeiget() {
	// ContentPanel panel = new ContentPanel();
	// panel.setHeaderVisible(false);
	// TableLayout tl = new TableLayout(2);
	// TableData td = new TableData();
	// td.setRowspan(1);
	// td.setColspan(1);
	// td.setPadding(5);
	//		
	// TableData td2 = new TableData();
	// td2.setRowspan(1);
	// td2.setColspan(2);
	// td2.setPadding(5);
	//		
	//		
	// panel.setLayout(tl);
	// panel.add(createGrid(),td);
	// panel.add(createGGrid(),td);
	// panel.add(createForm(),td2);
	// return panel;
	// }

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("busicode");
		type.getField("busicode").setType(String.class);
		type.addField("businame");
		type.getField("businame").setType(String.class);

		StoreInfo info = new StoreInfo();
		info.setTablename("business");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select icode,busicode,businame from business");
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	BaseGrid<BusiObject> grid = null;

	BaseGrid<BusiObject> createGrid() {
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C2 = new ColumnConfig("busicode", "业务编码", 80);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("businame", "业务名称", 80);
		columns.add(C3);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm, false, false);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setWidth(250);
		grid.setHeight(300);
		grid.getView().setForceFit(true);
		return grid;
	}

	// ---------

	BaseStore<BusiObject> gstore = null;

	BaseStore<BusiObject> getGStore() {
		if (gstore == null) {
			createGStore();
		}
		return gstore;
	}

	BaseStore<BusiObject> createGStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("busicode");
		type.getField("busicode").setType(String.class);
		type.addField("businame");
		type.getField("businame").setType(String.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("permissionsbcode");
		type.getField("permissionsbcode").setType(String.class);
		type.addField("canwrite");
		type.addField("canread");
		type.addField("canquery");
		type.getField("canwrite").setType(Boolean.class);
		type.getField("canread").setType(Boolean.class);
		type.getField("canquery").setType(Boolean.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("busipermissions");

		info.setKeyname(new String[] { "icode" });
		info
				.setSql("select icode,busicode,businame,bcode,permissionsbcode,canwrite,canread,canquery from busipermissions where busicode={busicode}");
		info.setParent(store);// 关联主表
		info.setLinkparentcolumns(new String[] { "busicode" }); // 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "busicode" });//
		// 关联主表字段
		gstore = new BaseStore<BusiObject>(type, info);
		return gstore;
	}

	BaseGrid<BusiObject> Ggrid = null;

	BaseGrid<BusiObject> createGGrid() {
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig C4 = new ColumnConfig("bcode", "操作人员", 80);
		C4.setRenderer(C_num4);
		columns.add(C4);
		ColumnConfig C5 = new ColumnConfig("permissionsbcode", "操作对象", 180);
		columns.add(C5);

		CheckColumnConfig canwrite = new CheckColumnConfig("canwrite", "可写", 30);
		CellEditor check_canwrite_BoxEditor = new CellEditor(new CheckBox());
		canwrite.setEditor(check_canwrite_BoxEditor);
		columns.add(canwrite);

		CheckColumnConfig canread = new CheckColumnConfig("canread", "可读", 30);
		CellEditor check_canread_BoxEditor = new CellEditor(new CheckBox());
		canread.setEditor(check_canread_BoxEditor);
		columns.add(canread);

		CheckColumnConfig canquery = new CheckColumnConfig("canquery", "可查询",
				40);
		CellEditor check_canquery_BoxEditor = new CellEditor(new CheckBox());
		canquery.setEditor(check_canquery_BoxEditor);
		columns.add(canquery);

		ColumnModel cm = new ColumnModel(columns);
		Ggrid = new BaseGrid<BusiObject>(createGStore(), cm);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		Ggrid.setSelectionModel(cbs);
		Ggrid.setWidth(600);
		Ggrid.setHeight(300);
		Ggrid.getView().setForceFit(true);
		return Ggrid;
	}

	BaseFormPanel createForm() {
		BaseFormPanel form = new BaseFormPanel(getGStore());
		form
				.setTopComponent(new StaticMessage(
						"“操作人员”是指登录系统的人员，“操作对象”是指单据由此人操作。<br>分配权限的意思是“操作人员”能对“操作对象”的单据有可读、写、查询的操作。",
						1));
		TableLayout tl = new TableLayout(7);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		form.setLayout(tl);
		form.setHeaderVisible(false);

		// form.add(new Label("icode:"), td);
		// NumberField C_num1 = new NumberField();
		// C_num1.setPropertyEditorType(Long.class);
		// C_num1.setName("icode");
		// form.add(C_num1, td);
		// form.add(new Label("busicode:"), td);
		// TextField<String> C_num2 = new TextField<String>();
		// C_num2.setName("busicode");
		// form.add(C_num2, td);
		// form.add(new Label("businame:"), td);
		// TextField<String> C_num3 = new TextField<String>();
		// C_num3.setName("businame");
		// form.add(C_num3, td);
		form.add(new Label("操作人员:"), td);
		//TextField<String> C_num4 = new TextField<String>();
		C_num4.setName("bcode");
	
		form.add(C_num4, td);
	

		form.add(new Label("操作对象:"), td);
		// -----------------------start
	/*	List<ColumnConfig> bocodecolumns = new ArrayList<ColumnConfig>();
		ColumnConfig bcode = new ColumnConfig("bcode", "人员代码", 120);

		bocodecolumns.add(bcode);

		ColumnConfig bname = new ColumnConfig("bname", "人员名称", 100);

		bocodecolumns.add(bname);

		ModelType combotype = new ModelType();
		combotype.setRoot("records");
		combotype.setRecordName("record");
		combotype.setTotalName("totle");
		// combotype.addField("code", "service");
		combotype.addField("bcode");
		combotype.addField("bname");

		StoreInfo comboinfo = new StoreInfo();
		comboinfo.setTablename("bcode");
		comboinfo.setForcenolimit("true");
		comboinfo
				.setSql("select bcode,bname from bcode where (bcode is not null or bname is not null)  order by bcode  ");

		MutiSelectEditor C_num5 = new MutiSelectEditor(bocodecolumns,
				combotype, comboinfo, "bcode");*/
		// -----------------------end

		//可以选择部门的选择框，对话框是暂时用的，因为此对话框值合适hr使用
		  bsd = new BcodeSelectDialog(){
				public void doposted(List modeldatalist) {
					String revalue = "";
					for(TreeNodeModelData bo:(List<TreeNodeModelData>)modeldatalist)
					{
						revalue+=bo.get("systembcode")+";";
					}
					setValue(revalue);
				}
		  };
		  bsd.setCheckNodes(CheckNodes.BOTH);
//		  bsd.setTarget_store(getGStore());
//		  bsd.setTarget_col(new ColumnInfo[]{new  ColumnInfo("bcode",ColumnInfo.STRING)});
//		  bsd.setSourcecol(new ColumnInfo[]{new ColumnInfo("bcode",ColumnInfo.STRING)});
//		  bsd.setFireChangeEventOnSetValue(true);
		bsd.setName("permissionsbcode");
		bsd.setWidth(200);
		form.add(bsd,td);
//		C_num5.setName("permissionsbcode");
//		form.add(C_num5, td);
		form.add(bsd, td);

		CheckBox C_num6 = new CheckBox();
		C_num6.setName("canwrite");
		C_num6.setBoxLabel("可写");
		form.add(C_num6, td);
		CheckBox C_num7 = new CheckBox();
		C_num7.setName("canread");
		C_num7.setBoxLabel("可读");
		form.add(C_num7, td);
		CheckBox C_num8 = new CheckBox();
		C_num8.setName("canquery");
		C_num8.setBoxLabel("可查询");
		form.add(C_num8, td);

		form.add(addrecord, td);
		form.add(delrecord, td);

		form.inited();
		return form;
	};

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if (be.getSource().equals(addrecord)) {
								getGStore().addX();
							} else if (be.getSource().equals(delrecord)) {
								Ggrid.deleterecord();
								getGStore().trySave();
							}
						} else if (be.getType() == Events.Change) {
							if (be.getSource().equals("输入型变量")) {

							}
						} else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}

}
